/**
 * 1.version:3.3 date:2023/3/1
 * 2.本文件仅适用于大疆电机CAN通信
 * 3.需要添加BSP和PID两个个文件
 */

#ifndef __DJ_MOTOR_DRIVER_H
#define __DJ_MOTOR_DRIVER_H

#include "main.h"
#include "PID.h"

#define DJ_MOTOR_CAN hcan1
#define DJ_M2006_DECRATIO 36.0f // M2006减速比
#define DJ_M3508_DECRATIO 19.0f // M3508减速比

// 电机类型
enum DJ_Motor_Type
{
    M3508 = 0,
    M2006 = 1
};

// 电机ID地址
enum CAN_Motor_ID
{
    DJ_ALL_ID = 0x200,
    DJ_M1_ID = 0x201,
    DJ_M2_ID = 0x202,
    DJ_M3_ID = 0x203,
    DJ_M4_ID = 0x204,
};

// 电机反馈参数
typedef struct
{
    uint16_t angle;
    int16_t speed;
    int16_t current;
    uint8_t temperature;
    uint16_t last_angle;

    uint8_t ID;
    uint8_t motor_type;
    float decratio;
    uint16_t offset_angle;
    float total_angle;
    int32_t round_count;

    PID_t PID_Speed;
    PID_t PID_Angle;
    PID_t PID_SpeedOfAngle;
} DJ_meature_t;

// CAN中断回调函数
void DJ_CAN_Callback(CAN_RxHeaderTypeDef *pHeader, uint8_t *pBuf);
// 电机初始化, 输入电机ID和电机类型
void DJ_Init(uint8_t Motor_ID, uint8_t Motor_Type);
// 发送电机电流
void DJ_SetCurrent(int16_t motor1_current, int16_t motor2_current, int16_t motor3_current, int16_t motor4_current);
void DJ_SetSpeed(int16_t *set_speed_array);
// 设置电机角度
void DJ_SetAngle(int16_t *set_angle_array);
// 获得电机编码器初始角度
void DJ_GetOffset(void);
// 电机角度清零
void DJ_ClearAngle(void);
// 返回电机回传信息
DJ_meature_t *DJ_GetMeasure(uint8_t ID);

#endif
